home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / direct pixel access / direct pixel access.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  6.6 KB  |  257 lines

  1. /*
  2.     File:        Direct Pixel Access.c
  3.  
  4.     Contains:    This snippet shows one example of how to directly            
  5.                 change the pixel values stored in a pixel image.            
  6.                 The original pixel image is obtained from a 'icl8'            
  7.                 resource.  Only the first 20 columns of the first        
  8.                 20 rows of the 'icl8' image is used.    
  9.  
  10.     Written by: Edgar Lee    
  11.  
  12.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 7/9/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 
  25.  
  26. */
  27.  
  28. #include <Dialogs.h>
  29. #include <Fonts.h>
  30. #include <Resources.h>
  31. #include <QDOffscreen.h>
  32. #include <TextUtils.h>
  33. /* Constant Declarations */
  34.  
  35. #define    WWIDTH    600
  36. #define    WHEIGHT    400
  37.  
  38. #define WLEFT    (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  39. #define WTOP    (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  40.  
  41. /* Global Variable Definitions */
  42.  
  43. WindowPtr        gWindow;
  44. PixMapHandle    gPixmap;
  45.  
  46. void initMac();
  47.  
  48. void createWindow();
  49. void createOffscreen();
  50. void drawPixelImageData();
  51. void doEventLoop();
  52.  
  53. void main(void)
  54. {
  55.     initMac();
  56.     
  57.     createWindow();
  58.     createOffscreen();
  59.     
  60.     doEventLoop();
  61. }
  62.  
  63. void initMac()
  64. {
  65.     MaxApplZone();
  66.  
  67.     InitGraf( &qd.thePort );
  68.     InitFonts();
  69.     InitWindows();
  70.     InitMenus();
  71.     TEInit();
  72.     InitDialogs( nil );
  73.     InitCursor();
  74.     FlushEvents( 0, everyEvent );
  75. }
  76.  
  77. void createWindow()
  78. {
  79.     Rect        rect;
  80.  
  81.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  82.     gWindow = NewCWindow( 0L, &rect, "\pDirect Pixel Access", true, documentProc,
  83.                             (WindowPtr)-1L, true, 0L );                        
  84.     SetPort( gWindow );
  85.     
  86.     TextFont( kFontIDGeneva );
  87.     TextSize( 9 );
  88.     TextMode( srcXor );
  89. }
  90.  
  91. void createOffscreen()
  92. {
  93.     Rect        rect;
  94.     Handle        iclHandle;
  95.     
  96.     SetRect( &rect, 0, 0, 32, 32 );
  97.     
  98.     /* Create offscreen pixmap image using an 'icl8' icon resource. */
  99.  
  100.     iclHandle = GetResource( 'icl8', 129 );
  101.     HLock( iclHandle );
  102.     HNoPurge( iclHandle );
  103.     
  104.     gPixmap = (PixMapHandle)NewHandle( sizeof( PixMap ) );
  105.     
  106.     (**gPixmap).baseAddr = *iclHandle;
  107.     (**gPixmap).rowBytes = ((32 * 8) / 8) | 0x8000;
  108.     (**gPixmap).bounds = rect;
  109.     (**gPixmap).pmVersion = 0;
  110.     (**gPixmap).packType = 0;
  111.     (**gPixmap).packSize = 0;
  112.     (**gPixmap).hRes = 72;
  113.     (**gPixmap).vRes = 72;
  114.     (**gPixmap).pixelSize = 8;
  115.     (**gPixmap).planeBytes = 0;
  116.     (**gPixmap).pmReserved = 0;
  117.     (**gPixmap).pixelType = 0;
  118.     (**gPixmap).cmpCount = 1;
  119.     (**gPixmap).cmpSize = 8;
  120.     (**gPixmap).pmTable = GetCTable( 8 );
  121.     
  122.     /* Give a unique seed for the pixmap's colortable. */
  123.     (**(**gPixmap).pmTable).ctSeed = GetCTSeed();
  124. }
  125.  
  126. void drawPixelImageData()
  127. {
  128.     int                row, col;
  129.     Rect            rect;
  130.     unsigned char    value;
  131.     char            *image;
  132.     int                index = 0;
  133.     Str255            string;
  134.     RGBColor        color = { 32000, 32000, 32000 };
  135.     Byte            mode;
  136.     
  137.     ForeColor( blackColor );
  138.     
  139.     /* For this example, let's just use only the upper left corner of the image. */
  140.     SetRect( &rect, 0, 0, 20, 20 );
  141.     
  142.     /* Set the pointer to the beginning of the pixel image. */
  143.     image = GetPixBaseAddr( gPixmap );
  144.     
  145.     /*********************************************************************************/
  146.     /* Switch into 32-bit addressing mode before accessing the pixel image directly. */
  147.     /*********************************************************************************/
  148.  
  149.     mode = true32b;
  150.     SwapMMUMode( (SInt8*)&mode );
  151.     
  152.     /*****************************************************************/
  153.     /* For this example, let's set the pixel values of the left half */
  154.     /*   of the image to half their original values.                 */
  155.     /*****************************************************************/
  156.     
  157.     for (row = 0; row < rect.bottom; row++)
  158.     {
  159.         /* Loop through the first 10 columns of the pixel image. */
  160.         for (index = 0, col = 0; col < rect.right / 2; col++)
  161.         {
  162.             /* Set the value at this index to half its value. */
  163.             value = (unsigned char)*(image + index);
  164.             *(image + index) = value / 2;
  165.             
  166.             index++;
  167.         }
  168.         
  169.         /* Increment the pointer to the next row of the pixel image. */
  170.         image += ((**gPixmap).rowBytes & 0x7fff);
  171.     }
  172.     
  173.     /********************************************************************/
  174.     /* Switch back to 24-bit addressing when done accessing the pixels. */
  175.     /********************************************************************/
  176.  
  177.     SwapMMUMode( (SInt8*)&mode );
  178.     
  179.     /* Draw the offscreen image to the screen to see what it looks like. */
  180.     CopyBits( (BitMap *)*gPixmap, &gWindow->portBits, &rect,
  181.             &gWindow->portRect, srcCopy, 0 );
  182.     
  183.     RGBForeColor( &color );
  184.     
  185.     /* Again, set the pointer to the beginning of the pixel image. */
  186.     image = GetPixBaseAddr( gPixmap );
  187.     
  188.     /***************************************************************/
  189.     /* Finally let's display the pixel values on top of the image. */
  190.     /***************************************************************/
  191.     
  192.     /* Loop through the first 20 rows of the pixel image. */
  193.     for (row = 0; row < rect.bottom; row++)
  194.     {
  195.         /* Loop through the first 20 columns of the pixel image. */
  196.         for (index = 0, col = 0; col < rect.right; col++)
  197.         {
  198.             /* Get the value at this index into the pixel image. */
  199.             value = (unsigned char)*(image + index);
  200.             
  201.             MoveTo( col * 30, row * 20 );
  202.             LineTo( col * 30, (row + 1) * 20 );
  203.             LineTo( (col + 1) * 30, (row + 1) * 20 );
  204.             
  205.             MoveTo( (col * 30) + 6, (row * 20) + 14 );
  206.             NumToString( (long)value, string );
  207.             DrawString( string );
  208.             
  209.             index++;
  210.         }
  211.         
  212.         /* Increment the pointer to the next row of the pixel image. */
  213.         image += ((**gPixmap).rowBytes & 0x7fff);
  214.     }
  215. }
  216.  
  217. void doEventLoop()
  218. {
  219.     EventRecord event;
  220.     WindowPtr   window;
  221.     short       clickArea;
  222.     Rect        screenRect;
  223.  
  224.     for (;;)
  225.     {
  226.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  227.         {
  228.             if (event.what == mouseDown)
  229.             {
  230.                 clickArea = FindWindow( event.where, &window );
  231.                 
  232.                 if (clickArea == inDrag)
  233.                 {
  234.                     screenRect = (**GetGrayRgn ()).rgnBBox;
  235.                     DragWindow( window, event.where, &screenRect );
  236.                 }
  237.                 else if (clickArea == inContent)
  238.                 {
  239.                     if (window != FrontWindow())
  240.                         SelectWindow( window );
  241.                 }
  242.                 else if (clickArea == inGoAway)
  243.                     if (TrackGoAway( window, event.where ))
  244.                         return;
  245.             }
  246.             else if (event.what == updateEvt)
  247.             {
  248.                 window = (WindowPtr)event.message;    
  249.                 SetPort( window );
  250.                 
  251.                 BeginUpdate( window );
  252.                 drawPixelImageData();
  253.                 EndUpdate( window );
  254.             }
  255.         }
  256.     }
  257. }